package com.gitee.grower.core.ui

import com.germ.germplugin.api.dynamic.gui.GermGuiScreen
import com.gitee.grower.util.germ.*
import ink.ptms.chemdah.util.conf
import org.bukkit.entity.Player
import org.bukkit.inventory.ItemStack
import taboolib.common.util.replaceWithOrder
import taboolib.library.configuration.ConfigurationSection
import taboolib.module.nms.getName

class GermChildUI(val viewer: Player, val item: ItemStack, val description: List<String>, val type: String) {

    fun isSuccessful() = type == "success"

    fun isFailed() = !isSuccessful()

    companion object {

        val config: ConfigurationSection
            get() = GermLogicUI.config.getConfigurationSection("grower-child-ui")!!

        val submit: ConfigurationSection
            get() = config.getConfigurationSection("submit")!!

        val mainSlot: ConfigurationSection
            get() = config.getConfigurationSection("main-slot")!!

        val itemTitle: ConfigurationSection
            get() = config.getConfigurationSection("item-title")!!

        val successText: String
            get() = itemTitle.getString("success-text")!!

        val failText: String
            get() = itemTitle.getString("success-text")!!

        val description: ConfigurationSection
            get() = config.getConfigurationSection("description-text")!!

        val lightSuccess: ConfigurationSection
            get() = config.getConfigurationSection("success-light")!!

        val lightFail: ConfigurationSection
            get() = config.getConfigurationSection("fail-light")!!
    }

    fun openChild() {
        val screen = createGUI {
            it.infuse(config)
            it.parts += createDSL<GermSlotDSL>(mainSlot) {
                it.item = this.item
                it.identity = "@GrowerResult"
            }
            it.map["@GrowerType"] = type
            it.parts += createDSL<GermLabelDSL>(itemTitle) {
                if (this.isSuccessful()) {
                    it.texts += successText.replaceWithOrder(this.item.getName())
                } else {
                    it.texts += failText.replaceWithOrder(this.item.getName())
                }
            }
            it.parts += createDSL<GermLabelDSL>(GermChildUI.description) {
                it.texts += this.description
            }
            it.parts += createDSL<GermTextureDSL>(if (isSuccessful()) lightSuccess else lightFail) {
                it.enable = true
            }
            it.parts += createDSL<GermButtonDSL>(submit) {
                it.onClick {
                    it.close()
                }
            }
        }
        screen.openChildGui(viewer)
    }


}